﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MonoTouchFlavePackageProject.cs" company="Roman '4ux-nbIx' Novitsky">
//   Copyright (c) 2012 Roman '4ux-nbIx' Novitsky
// </copyright>
// <summary>
//   Defines the MonoTouchFlavePackageProject type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MonoDevAssist
{
  #region Namespace Imports

  using System;
  using System.Drawing;
  using System.IO;
  using System.Runtime.InteropServices;
  using System.Windows.Forms;

  using Microsoft.VisualStudio;
  using Microsoft.VisualStudio.Shell.Flavor;
  using Microsoft.VisualStudio.Shell.Interop;

  #endregion


  [ComVisible(true)]
  [ClassInterface(ClassInterfaceType.None)]
  [Guid(_packageProjectGuid)]
  internal class MonoTouchFlavePackageProject : FlavoredProjectBase
  {
    #region Constants and Fields

    private const string _packageProjectGuid = "628E6A0A-36B0-4a79-BB2E-3E1B3BB38C82";

    private readonly MonoDevAssistPackage _package;
    private static int _iconIndex;
    private static ImageList _imageList;

    #endregion


    #region Constructors and Destructors

    public MonoTouchFlavePackageProject(MonoDevAssistPackage package)
    {
      _package = package;
    }

    #endregion


    #region Methods

    protected override int GetProperty(uint itemId, int propId, out object property)
    {
      if (itemId == VSConstants.VSITEMID_ROOT)
      {
        if (propId == (int)__VSHPROPID.VSHPROPID_IconIndex)
        {
          property = _iconIndex;
          return VSConstants.S_OK;
        }

        if (propId == (int)__VSHPROPID.VSHPROPID_OpenFolderIconIndex)
        {
          property = _iconIndex;
          return VSConstants.S_OK;
        }

        if (propId == (int)__VSHPROPID.VSHPROPID_IconImgList)
        {
          if (_imageList == null)
          {
            base.GetProperty(itemId, (int)__VSHPROPID.VSHPROPID_IconImgList, out property);

            _imageList = ImageListConverter.FromHandle(new IntPtr((int)property));

            _iconIndex = _imageList.Images.Count;

            Image icon = GetProjectIcon();

            _imageList.Images.Add(icon);
            _imageList.Images.Add(icon);
          }

          property = _imageList.Handle;

          return VSConstants.S_OK;
        }
      }

      return base.GetProperty(itemId, propId, out property);
    }


    protected override void SetInnerProject(IntPtr innerIUnknown)
    {
      if (serviceProvider == null)
      {
        serviceProvider = _package;
      }

      base.SetInnerProject(innerIUnknown);
    }


    private static Image GetProjectIcon()
    {
      Stream stream =
        typeof(MonoTouchFlavePackageProject).Assembly.GetManifestResourceStream("MonoDevAssist.Resources.iphone.png");

      if (stream == null)
      {
        return null;
      }

      Image image = Image.FromStream(stream);
      return image;
    }

    #endregion
  }
}